package org.client.view.manage;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.client.tools.ClientTool;
import org.client.ui.IMChatView;
import org.pojo.MessageType;
import org.pojo.User;
import org.pojo.Message.Message;
public class Client2ServerThread extends Thread {


	public static Socket s;
	public IMChatView chat;
	public static ObjectOutputStream oos;
	public static ObjectInputStream ois;
	//构造函数
	public Client2ServerThread(Socket s,ObjectOutputStream oos, ObjectInputStream ois)
	{
		this.oos = oos;
		this.ois = ois;
		this.s=s;
	}
	
	public void run()
	{
		//第一次请求更新
		//发送一个要求返回在线人请求包.
		//做一个Message
		Message m=new Message();
		m.setMesType(MessageType.message_get_onLineFriend);
		try {
			oos.writeObject(m);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(true)
		{	

			//不停的读取从服务器端发来的消息	
			try {
				//ObjectInputStream oiss;
				//oiss = new ObjectInputStream(s.getInputStream());
				m = (Message)ois.readObject();				
				if(m.getMesType().equals(MessageType.message_comm_textmsg))
				{
					File wavFile = new File("music/qq.wav");
					AudioInputStream ais = AudioSystem.getAudioInputStream(wavFile);
					Clip  clip = AudioSystem.getClip();
					clip.open(ais);
					clip.start();
					if((chat = ClientTool.clientviews.get(m.getSenderId()))!=null){
						//显示
						chat.showMessage(m);
					}
					else{
						ClientTool.mainWnd.AcceptChat(m);
					}

				}else if(m.getMesType().equals(MessageType.message_ret_onLineFriend))
				{

					if(m.getCon() instanceof User){
						Iterator<User> local=ClientTool.onlinefriends.iterator();
						User user=(User)m.getCon(); 
						boolean b = false;
						while(local.hasNext()){
							User my = local.next();
							if(my.getUserid() == user.getUserid()){
								b = true;
								ClientTool.onlinefriends.remove(my);
								ClientTool.onlinefriends.add(user);		
								ClientTool.mainWnd.updateExistFriend();
								System.out.println("更新好友列表成功！！");
								break;
							}
						}
						if(!b){
							List<User> u = new LinkedList<User>();
							u.add(user);
							//修改相应的好友列表.
							ClientTool.onlinefriends.add(user);
							ClientTool.mainWnd.updateOnlineFriends(u);
						}
						
					}
					else if(m.getCon() instanceof List<?>){
						List<User> user = (List<User>)m.getCon();
						ClientTool.onlinefriends = user;
						ClientTool.mainWnd.updateOnlineFriends(user);
					}
					System.out.println("更新好友列表成功！！");
				}else if(m.getMesType().equals(MessageType.message_ret_offLineFriend)){
					
					User u=(User)m.getCon();
					ClientTool.onlinefriends.remove(u);
					ClientTool.mainWnd.updateOfflineFriends(u);
					
				}else if(m.getMesType().equals(MessageType.message_update_personinfo)){
					
					ClientTool.currentUser = (User)m.getCon();
					ClientTool.mainWnd.updateMyInfo();
					
				}else if(m.getMesType().equals(MessageType.message_closesocket)){
					oos.close();
					ois.close();
					s.close();
					break;
				}	

			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}	
		
	}
}
